package bravo.xc.com.xc_bravo_android.operation.re_password;

import android.util.Log;

import bravo.xc.com.xc_bravo_android.beans.ForgetBeans;
import bravo.xc.com.xc_bravo_android.beans.ForgetInitResultBeans;
import bravo.xc.com.xc_bravo_android.beans.SendCodeBeans;
import bravo.xc.com.xc_bravo_android.beans.SendCodeResultBeans;
import bravo.xc.com.xc_bravo_android.http.HttpSingleton;
import bravo.xc.com.xc_bravo_android.http.UserOperationService;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * CREATED BY YANK
 * DATE : 2017/10/28
 * MAIL : YANK.TENYOND@GMAIL.COM
 * FUNCTION :
 */

public class RePasswordPresenter extends RePasswordContract.RePasswordPresenter {

    private UserOperationService service = HttpSingleton.getInstance().service;

    @Override
    public void doRepassword(ForgetBeans forgetBeans) {

        compositeSubscription.add(service.forget(forgetBeans)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<ForgetInitResultBeans>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        Log.e("@@onError",String.valueOf(e));
                        view.showProgress(false);
                        view.showToast("出现未知错误,请重试");
                    }

                    @Override
                    public void onNext(ForgetInitResultBeans forgetInitResultBeans) {
                        view.showProgress(false);
                        if(forgetInitResultBeans.getCode() == 0){
                            view.showToast("获取失败");
                        }
                        if(forgetInitResultBeans.getCode() == 1){
                            view.showToast("密码修改成功");
                            view.toNewActivity();
                        }
                        if(forgetInitResultBeans.getCode() == 2){
                            view.showToast("修改失败");
                        }
                        if(forgetInitResultBeans.getCode() == 3){
                            view.showToast("验证码错误");
                        }
                    }
                }));

    }

    @Override
    public void dosendCode(SendCodeBeans codeBeans) {

        compositeSubscription.add(service.sendCode(codeBeans)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<SendCodeResultBeans>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        Log.e("@@onError",String.valueOf(e));
                        view.showProgress(false);
                        view.showToast("出现未知错误,请重试");
                    }

                    @Override
                    public void onNext(SendCodeResultBeans sendCodeResultBeans) {

                        if(sendCodeResultBeans.getCode() == 1){
                            view.showToast(sendCodeResultBeans.getMsg());
                        }
                        view.showProgress(false);
                    }
                }));


    }

    @Override
    public void doGetInit(SendCodeBeans codeBeans) {

        compositeSubscription.add(service.forgetInit(codeBeans)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<ForgetInitResultBeans>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        Log.e("@@onError",String.valueOf(e));
                        view.showProgress(false);
                        view.showToast("出现未知错误,请重试");

                    }

                    @Override
                    public void onNext(ForgetInitResultBeans forgetInitResultBeans) {
                        if(forgetInitResultBeans.getCode() == 1){
                            view.showToast("获取手机号成功");
                            view.setInfo(forgetInitResultBeans.getData().get(0).getUsername(),forgetInitResultBeans.getData().get(0).getPhoneNum());
                        }
                        if(forgetInitResultBeans.getCode() == 2){
                            view.showToast("请检查您的用户名");
                        }
                        view.showProgress(false);
                    }
                }));
    }
}
